package net.expectx.pay.controller.system;

import io.swagger.annotations.ApiOperation;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.dao.bean.SystemLog;
import net.expectx.pay.dao.bean.SystemLogExample;
import net.expectx.pay.dao.bean.SystemTypeGroup;
import net.expectx.pay.dao.bean.SystemTypeGroupExample;
import net.expectx.pay.rpc.api.SystemLogService;
import net.expectx.pay.rpc.api.SystemSettingService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hxs
 */
@Controller
@RequestMapping(value = "/manager/system/systemLog")
public class SystemLogController {
    private static final Logger LOGGER = Logger.getLogger(SystemSettingController.class);
    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation(value = "日志主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "system/systemLog/index";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "requestType") String requestType
            ,String userName) {
        Map<String, Object> result = new HashMap<>(16);

        SystemLogExample systemLogExample = new SystemLogExample();
        /**
         * 创造条件
         */
        SystemLogExample.Criteria criteria=systemLogExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        if (org.apache.commons.lang.StringUtils.isNotBlank(requestType)) {
            criteria.andRequestTypeEqualTo(requestType);
        }
        if (StringUtils.isNotBlank(userName)) {
            criteria.andUserNameEqualTo(userName);
        }
        /**
         * 分页
         */
        systemLogExample.setPageIndex(page);
        systemLogExample.setPageSize(limit);
        /**
         * 排序
         */
        systemLogExample.setOrderByClause("gmt_modified desc");

        List<SystemLog> systemLogs = systemLogService.selectByExample(systemLogExample);
        long total = systemLogService.countByExample(systemLogExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", systemLogs);
        return result;
    }


}
